﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GamePanel : PanelBase
{
    private Button BombButton;//炸弹技能按钮
    //private Button MassButton;//集结钮按
    //private Button SoldierAttackButton;//集结按钮

    private Button BarracksCreat;//创建兵营按钮
    //private Button MineFieldCreat;

    private bool BombSkillisActive = false;//判断炸弹按钮是否激活
    private bool BarrackisActive = false;//判断兵营按钮是否激活
    //private bool MineFieldisActive;//判断兵营按钮是否激活


    public Text coinNumber;//金币余量
    public Text SoldierNumber;//兵力余量
    public Text PlayerBlood;
    public Image PlayerBar;
    public Text EnemyBlood;
    public Image EnemyBar;


    //GameObject tempRange;

    #region 生命周期
    public override void Init(params object[] args)
    {
        skin = this.gameObject;
        base.Init(args);
        skinPath = "Prefabs/UI/GamePanel";//定义皮肤skinPath
        layer = PanelMgr.PanelLayer.Panel;//定义层级layer
        

    }
    public override void OnShowing()
    {
        base.OnShowing();
        
        BombButton = skin.transform.Find("BombSkill/BombButton").GetComponent<Button>();
        coinNumber = skin.transform.Find("CoinNumber").GetComponent<Text>();
        SoldierNumber = skin.transform.Find("SoldierNumber").GetComponent<Text>();

        PlayerBlood = skin.transform.Find("PlayerBlood/playerBlood").GetComponent<Text>();
        PlayerBar = skin.transform.Find("PlayerBlood/PlayerBar").GetComponent<Image>();
        EnemyBlood = skin.transform.Find("EnemyBlood/enemyBlood").GetComponent<Text>();
        EnemyBar = skin.transform.Find("EnemyBlood/EnemyBar").GetComponent<Image>();

        BarracksCreat = skin.transform.Find("backgrounddown/ButtonGrid/BarracksCreat").GetComponent<Button>();
        //MineFieldCreat = DownPanel.transform.Find("MineFieldCreat").GetComponent<Button>();

        BombButton.onClick.AddListener(delegate ()
        {
            // TODO：炸弹技能激活
            Debug.Log("BombButton");
            //if (Resource.instance.coinNum >= SkillManager.instance.takeMoney)
            //{
            //    BombSkillisActive = !BombSkillisActive;
            //}
            BombSkillisActive = !BombSkillisActive;
        });
        BarracksCreat.onClick.AddListener(delegate ()
        {
            Debug.Log("BarracksCreat");
            //if (Resource.instance.coinNum >= new Barrack().price)
            //{
            BarrackisActive = !BarrackisActive;
            //}
        });
        //MineFieldisActive = false;

        //MassButton = UpPanel.transform.Find("SkillButtonPanel/MassButton").GetComponent<Button>();
        //SoldierAttackButton = UpPanel.transform.Find("SkillButtonPanel/SoldierAttackButton").GetComponent<Button>();






        //设置监听按钮点击事件

        //MassButton.onClick.AddListener(delegate ()
        //{
        //    Debug.Log("MessBtton");
        //});
        //SoldierAttackButton.onClick.AddListener(delegate ()
        //{
        //    Debug.Log("SoldierAttackButton");
        //});

        //MineFieldCreat.onClick.AddListener(delegate ()
        //{
        //    Debug.Log("MineFieldCreat");
        //    if (Resource.instance.coinNum >= new MineField().price)
        //    {
        //        MineFieldisActive = !MineFieldisActive;
        //    }
        //});
    }
    #endregion
    // Update is called once per frame
    public override void Update()
    {
        if (BombSkillisActive)
        {
            GameJudge.instance.Player1.GetComponent<SkillManager>().BombSkill(ref BombSkillisActive);
        }

        if (BarrackisActive)
        {
            UnitManager.instance.CreatBarrack(ref BarrackisActive);
        }

        //if (MineFieldisActive)
        //{
        //    UnitManager.getInstance().CreatMineField(ref tempRange, ref MineFieldisActive);
        //}

        if (coinNumber != null)
        {
            if (GameJudge.instance.Player1 != null)
                coinNumber.text = GameJudge.instance.Player1.GetComponent<Resource>().coinNum.ToString();
        }
        if (SoldierNumber != null)
        {
            if (GameJudge.instance.Player1 != null)
                SoldierNumber.text = GameJudge.instance.Player1.GetComponent<Resource>().soldierNum.ToString();
        }
        if (PlayerBlood != null)
        {
            if (GameJudge.instance.Player1 != null)
            {
                PlayerBlood.text = GameJudge.instance.Player1.GetComponent<Camp>().currentHP.ToString();
                if (PlayerBar != null)
                {
                    PlayerBar.fillAmount = GameJudge.instance.Player1.GetComponent<Camp>().currentHP / GameJudge.instance.Player1.GetComponent<Camp>().MaxHp;
                }
            }
        }
        if (EnemyBlood != null)
        {
            if (GameJudge.instance.Player2 != null)
            {
                EnemyBlood.text = GameJudge.instance.Player2.GetComponent<Camp>().currentHP.ToString();
                if (EnemyBar != null)
                {
                    EnemyBar.fillAmount = GameJudge.instance.Player2.GetComponent<Camp>().currentHP / GameJudge.instance.Player2.GetComponent<Camp>().MaxHp;
                }
            }
        }
    }
}
